package com.eportal.struts.interceptor;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.omg.CORBA.Request;

import com.eportal.ORM.MpayUser;
import com.eportal.ORM.UserView;
import com.eportal.ORM.UserView;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginedCheckInterceptor extends AbstractInterceptor {

	private String msg;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		super.init();
		System.out.println("init listen server");
	}

	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("开始拦截。。。");
		UserView o = (UserView) arg0.getInvocationContext().getSession().get(
				"mpay_user");
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		if (o == null) {
			System.out.println("session超时。。。");
			setMsg("timeout");
			if (request.getHeader("x-requested-with") != null
					&& request.getHeader("x-requested-with").equalsIgnoreCase(
							"XMLHttpRequest")) {
				response.setHeader("sessionstatus", "timeout");
			}
			request.setAttribute("msg", "登录超时，请重新登录！");
			return "timeout";
		}
		System.out.println(o.getMpayUserid() + "_session正常。。。");
		return arg0.invoke();
	}

}
